package com.example.zhaojianxu.signin.common.info

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.common.account.model.UserEntity
import com.example.zhaojianxu.signin.common.constance.ComConstance
import com.example.zhaojianxu.signin.common.http.HttpUtil
import com.example.zhaojianxu.signin.common.util.ToastUtil
import kotlinx.android.synthetic.main.activity_info.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class InfoActivity : BaseActivity() {

    override fun initView() {
        common_back.setOnClickListener {
            finish()
        }

        common_title.text = "个人信息"
        common_setting.text = "保存"
        common_setting.setTextColor(Color.WHITE)
        common_setting.setOnClickListener {
            if (activity_info_1.text.isNotEmpty()) {
                updateInfo()
            }
        }
    }

    override fun initData() {
        HttpUtil.instance.getCommonApi()?.postQueryInfo(ComConstance.UID)?.enqueue(object : Callback<UserEntity> {
            override fun onFailure(call: Call<UserEntity>, t: Throwable) {
                Log.e("EGInfoActivity", "${t.message}")

            }

            override fun onResponse(call: Call<UserEntity>, response: Response<UserEntity>) {
                if (response.isSuccessful && response.body() != null) {
                    activity_info_1.setText(response.body()?.name)
                    activity_info_2.setText(response.body()?.number)
                    activity_info_3.setText(response.body()?.password)
                }
            }

        })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_info)
    }

    fun updateInfo() {
        HttpUtil.instance.getCommonApi()?.postUpdateInfo(ComConstance.UID, activity_info_1.text.toString(),activity_info_2.text.toString(),activity_info_3.text.toString())
            ?.enqueue(object : Callback<UserEntity> {
                override fun onFailure(call: Call<UserEntity>, t: Throwable) {
                    Log.e("EGInfoActivity", "${t.message}")

                }

                override fun onResponse(call: Call<UserEntity>, response: Response<UserEntity>) {
                    if (response.isSuccessful && response.body() != null) {
                        activity_info_1.setText(response.body()?.name)
                        ToastUtil.instance.showToast(this@InfoActivity,"修改成功")
                        finish()
                    }
                }

            })
    }
}
